//
//  BKXEmotionToolBar.swift
//  微博
//
//  Created by 田凯 on 16/4/25.
//  Copyright © 2016年 田凯. All rights reserved.
//

import UIKit

enum emotionToolBarType: Int{
    case defaults = 0
    case enoji = 1
    case lxh = 2
}
class BKXEmotionToolBar: UIView {
    
    
    // 点击底部button的闭包
    var buttonClickBag:((type: emotionToolBarType)->())?
    
    // 中间变量 button
    var currentButton: UIButton?
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    func setupUI(){
    
        // 添加控件button
        
        let defaultButton = addButton("compose_emotion_table_left", title: "默认",type:.defaults)
        let emojiButton = addButton("compose_emotion_table_mid", title: "emoji",type: .enoji)
        let lxhButton = addButton("compose_emotion_table_right", title: "浪小花",type: .lxh)
        // 添加约束
        
        defaultButton.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(emojiButton)
        }
        emojiButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(defaultButton.snp_trailing)
            make.width.equalTo(lxhButton)
        }
        lxhButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emojiButton.snp_trailing)
        }
        
        
    }
    
    
    // MARK: -  button的点击事件
    func buttonClick(button: UIButton){
    
        if button == currentButton{
        
        return
        }
        button.selected = true
        currentButton?.selected = false
        currentButton = button
        
        //执行闭包
        buttonClickBag?(type: emotionToolBarType(rawValue: button.tag)!)
    }
    
    
    
    //添加button的 公共 方法 compose_emotion_table_left_normal   compose_emotion_table_left_selected
    func addButton(imageName: String,title: String,type: emotionToolBarType)->UIButton{
    let  button = UIButton()
        //添加button的点击事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 设置tag 值
        button.tag = type.rawValue
        button.setTitle(title, forState: UIControlState.Normal)
        //设置 文字颜色
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.greenColor(), forState: UIControlState.Selected)
        //设置文字大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        // 设置图片
        button.setBackgroundImage(UIImage(named: "\(imageName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(imageName)_selected"), forState: UIControlState.Selected)
        button.sizeToFit()
        addSubview(button)
        
        return button
        
    
    
    }
    
}
